package com.example.team01_task21;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service {

	private static final String LOGCAT = null;
	MediaPlayer AudioPlayer;
	long finalTime, startTime;

	public void onCreate() {
		super.onCreate();
		Log.d(LOGCAT, "Service Started!");

		AudioPlayer = MediaPlayer.create(this, R.raw.thuychi);

		finalTime = AudioPlayer.getDuration();
		startTime = AudioPlayer.getCurrentPosition();
	}

	public int onStartCommand(Intent intent, int flags, int startId) {
		AudioPlayer.start();

		Log.d(LOGCAT, "Media Player started!");
		if (AudioPlayer.isLooping() != true) {
			Log.d(LOGCAT, "Problem in Playing Audio");
		}
		return START_STICKY;
	}

	public void onStop() {
		AudioPlayer.stop();
		AudioPlayer.release();
	}

	public void onPause() {
		AudioPlayer.stop();
		AudioPlayer.release();
	}

	public void onDestroy() {
		AudioPlayer.stop();
		AudioPlayer.release();
	}

	private final IBinder mBinder = new LocalBinder();

	/**
	 * Class used for the client Binder. Because we know this service always
	 * runs in the same process as its clients, we don't need to deal with IPC.
	 */
	public class LocalBinder extends Binder {
		MyService getService() {
			// Return this instance of LocalService so clients can call public
			// methods
			return MyService.this;
		}
	}

	@Override
	public IBinder onBind(Intent intent) {
		return mBinder;
	}

	public void plauseAudio() {
		if (AudioPlayer.isPlaying())
			AudioPlayer.pause();

	}

	public long getFinalTime() {
		return finalTime;
	}

	public void setFinalTime(long finalTime) {
		this.finalTime = finalTime;
	}

	public long getStartTime() {
		if (AudioPlayer.isPlaying()) {
			return AudioPlayer.getCurrentPosition();
		}
		return 0;
	}

	public void setStartTime(long startTime) {
		this.startTime = startTime;
	}

}